package cn.itsource.controller;

import cn.itsource.domain.User;
import cn.itsource.service.IUserService;
import cn.itsource.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public JSONResult<User> login(@RequestBody User user){
        try{
            //根据用户名和密码进行登录响应token
            String token = userService.login(user.getUsername(), user.getPassword());
            return JSONResult.ok("登录成功",token);
        }catch (RuntimeException e){
            e.printStackTrace();
            return JSONResult.fail(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.fail("系统正在升级,请稍后再试!!");
        }
    }
}
